interface Player {
  bankerCount: number,
  lftId: number,
  nickname: string,
  owner: boolean,
  point: number,
  seatIndex: number,
  suitDescribe: Array<{name: string, count: number}>,
  suitMap: any,
  suitPatterns: any,
  uid: string,
  winner: boolean
}

declare var window: any

class UserLabel extends eui.Component {
  private userGroup: eui.Group
  private ownerGroup: eui.Group
  private labelBg: eui.Image
  private useridGroup: eui.Group
  private userContent: eui.Group
  private userScore: eui.Group
  private username: eui.Label
  private userid: eui.Label
  public player: Player
  constructor(player) {
    super()
    this.once(eui.UIEvent.COMPLETE, this.onComplete, this)
    this.player = player
    this.skinName = `<e:Skin class="MajiangSkin" width="636" height="190" xmlns:e="http://ns.egret.com/eui" xmlns:w="http://ns.egret.com/wing">
      <e:Image id="labelBg" x="0" y="0" width="636" height="190"></e:Image>
      <e:Group id="userGroup" width="636" height="190">
        <e:Group id="ownerGroup" x="20" y="30" percentWidth="100" height="43">
        </e:Group>
        <e:Label id="username" text="" textColor="0xfef088" size="26" left="70" top="33"
         percentWidth="80" textAlign="left" height="30"></e:Label>
        <e:Group id="useridGroup" x="0" y="33" percentWidth="100" height="43">
          <e:Label id="userid"  textColor="0xedefee" text="" size="26" height="43"></e:Label>
        </e:Group>
        <e:Group id="userContent" horizontalCenter="-180" x="0" y="80" width="240" height="64">
        </e:Group>
        <e:Group id="userScore" x="460" y="76" width="128" height="104">
        </e:Group>
      </e:Group>
    </e:Skin>`
  }

  scoreNumber(point): Array<string> {
    var pointSigned = point > 0 ? '+' + point : point
    var numbers = (pointSigned + '').split('')
    return numbers.map(num => {
      if (point >= 0) {
        return `${num}_png`
      }
      if (num === '-') {
        return '-_png'
      }
      return `1${num}_png`
    })
  }

  private onComplete(e: eui.UIEvent) {
    const player = this.player
    const bgSrc = player.point > 0 ? 'win_png' : 'lose_png'
    this.labelBg.source = bgSrc

    var hLayout:eui.HorizontalLayout = new eui.HorizontalLayout()
    hLayout.gap = 10
    // hLayout.height = 256
    hLayout.horizontalAlign = egret.HorizontalAlign.CENTER
    this.useridGroup.layout = hLayout   /// 水平布局


    var tLayout:eui.TileLayout = new eui.TileLayout();
    tLayout.horizontalGap = 10
    tLayout.verticalGap = 23
    tLayout.columnAlign = eui.ColumnAlign.JUSTIFY_USING_WIDTH
    tLayout.rowAlign = eui.RowAlign.JUSTIFY_USING_HEIGHT
    tLayout.paddingTop = 6
    tLayout.paddingRight = 0
    tLayout.paddingLeft = 10
    tLayout.paddingBottom = 0
    tLayout.requestedColumnCount = 3  /// 设置3列显示
    this.userContent.layout = tLayout //设置问垂直布局

    if (this.player.suitDescribe ) {
      if (this.player.suitDescribe.length === 4) {
        tLayout.requestedColumnCount = 2  /// 设置2显示
      }
    }

    this.player.suitDescribe.forEach(element => {
      const label = new eui.Label(`${element.name}:  ${element.count}`)
      label.size = 23
      label.textColor = 0x010101;
      this.userContent.addChild(label)
    })

    this.username.text = this.player.nickname.substring(0, 6)
    this.userid.text = 'ID: ' + this.player.lftId

    if (this.player.winner) {
      const winner = new eui.Image('big-winner_png')
      winner.right = -5
      winner.bottom = 0
      this.userGroup.addChild(winner)
    }
    if (this.player.owner) {
      let ownerImg = new eui.Image('owner_png')
      this.ownerGroup.addChild(ownerImg)
    }

    var hLayoutScore:eui.HorizontalLayout = new eui.HorizontalLayout()
    hLayoutScore.gap = 0
    hLayoutScore.paddingRight = 30
    hLayoutScore.horizontalAlign = egret.HorizontalAlign.CENTER
    this.userScore.layout = hLayoutScore   /// 水平布局

    const scoreNumber = this.scoreNumber(this.player.point)
    scoreNumber.forEach(number => {
      var num = new eui.Image(number)
      num.width = 32
      num.height = 42
      this.userScore.addChild(num)
    })

    if (this.$hasAddToStage) {
      window.eventDispatcher.dispatchEvent(new egret.Event('ok'))
    } else {
      this.once(eui.UIEvent.ADDED_TO_STAGE, function () {
        setTimeout(() => {
          window.eventDispatcher.dispatchEvent(new egret.Event('ok'))
        }, window.labelRenderTime)
      }, this)
    }
  }
}

export default UserLabel